//
//  WZDepListView.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/12/31.
//  Copyright © 2015年 liuyuantao. All rights reserved.
//

#import "WZDepListView.h"
#import "CXmppRequestMgr.h"
#import "CDepartmentModel.h"
#import "UIImage+ImageWithColor.h"
#import "WZDepListCell.h"

@interface WZDepListView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation WZDepListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.image = [UIImage strechingWithImageName:@"depListBackGround"];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }
    return self;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = ClearColor;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        [self addSubview:_tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.depListArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CDepartmentModel *model = self.depListArray[indexPath.row];
    WZDepListCell *cell = [WZDepListCell depListCellWithTableView:tableView];
    if (self.depListArray.count - 1 == indexPath.row && self.depListArray.count != 1) {
        cell.sepreateLine.hidden = YES;
        cell.textLabel.textColor = RGBCOLORWITH16COLOR(0xfb8d44);
    } else {
        if (self.depListArray.count == 1) {
           cell.textLabel.textColor = RGBCOLORWITH16COLOR(0xfb8d44);
        } else {
            cell.textLabel.textColor = RGBCOLORWITH16COLOR(0x323232);
        }
        cell.sepreateLine.hidden = NO;
    }
    cell.textLabel.text = model.DepartName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.depListDelegate respondsToSelector:@selector(depListViewDidSelectedWithDepModel:indexPath:)]) {
         CDepartmentModel *model = self.depListArray[indexPath.row];
        [self.depListDelegate depListViewDidSelectedWithDepModel:model indexPath:indexPath];
    }
}

- (void)showDepListView
{
    self.frame = CGRectMake(HARDWARE_SCREEN_WIDTH - 150.0f - 7.0f, 0, 150.0f, 0);
    [UIView animateWithDuration:0.3f animations:^{
        CGRect rec = self.frame;
        rec.size.height = self.depListArray.count * 44.0f + 5.0f;
        self.frame = rec;
        self.tableView.frame = CGRectMake(0, 5.0f, rec.size.width, rec.size.height- 5.0f);
    } completion:^(BOOL finished) {
        [self.tableView reloadData];
    }];
}

- (void)hieDepListView
{
    [UIView animateWithDuration:0.3f animations:^{
        CGRect rec = self.frame;
        rec.size.height = 0;
        self.frame = rec;
        self.tableView.frame = CGRectMake(0, 0, rec.size.width, rec.size.height);
    } completion:^(BOOL finished) {
        [self.tableView reloadData];
    }];

}
@end
